#!/bin/bash

BIN=/usr/bin/chromium

Chrome_Default_Cli=" --ignore-gpu-blocklist --no-first-run --password-store=basic --simulate-outdated-no-au='Tue, 31 Dec 2099 23:59:59 GMT' --start-maximized --user-data-dir"
Log_Op=''

# Cleanup
if ! pgrep chromium > /dev/null;then
  rm -f $HOME/.config/chromium/Singleton*
fi

if $(u has -e SOCAT); then
    # u _ED socat found, enabling debug port 
    socat tcp-listen:13011,reuseaddr,fork tcp:localhost:9222 &
    Chrome_Default_Cli+=" --remote-debugging-port=9222 --remote-debugging-address=0.0.0.0"
fi;

if $(u has -e LESSLOG); then
    u _ED suppressing chromium logs
    Log_Op='> /dev/null 2>&1'
fi;

if  ! grep -q 'Seccomp:.0' /proc/1/status || $(u string -e "$PUID" "0")  || $(u string -e "PGID" "0"); then
    u _ED enable no-sandbox args
    Chrome_Default_Cli+=" --no-sandbox --test-type"
fi;

if $(u has -e TUNED); then
    u _ED tuned found, adding more args to default cli
    Chrome_Default_Cli+=" --autoplay-policy=no-user-gesture-required --disable-software-rasterizer --mute-audio --disable-audio-input --disable-smooth-scrolling --disable-speech-api --disable-overscroll-edge-effect --disable-pull-to-refresh-effect --use-fake-device-for-media-stream --auto-accept-camera-and-microphone-capture --disable-notifications"
fi;

# $UA user-agent
case "${UA,,}" in
    "firefox")
        Chrome_Default_Cli+=" --user-agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2; rv:120.0) Gecko/20100101 Firefox/120.0'"
        ;;
    "safari")
        Chrome_Default_Cli+=" --user-agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 14.5) AppleWebKit/618.9 (KHTML, like Gecko) Version/17.4 Safari/618.9'"
        ;;
    "chrome")
        Chrome_Default_Cli+=" --user-agent='Mozilla/5.0 (Windows NT 6.4) AppleWebKit/537.36.0 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36.0'"
        ;;
    "ipad")
        Chrome_Default_Cli+=" --use-mobile-user-agent --user-agent='Mozilla/5.0 (iPad; CPU OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1'"
        ;;
    "ioschrome")
        Chrome_Default_Cli+=" --use-mobile-user-agent --user-agent='Mozilla/5.0 (iPad; CPU OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/127.0.6533.56 Mobile/15E148 Safari/604.1'"
	;;
    *)
        ;;
esac

if $(u has -e CHROME_CLI_PLAIN); then
    u _ED using plain cli as {$CHROME_CLI_PLAIN}
    eval "$BIN $CHROME_CLI_PLAIN $Log_Op";
elif $(u has -e CHROME_CLI); then
    u _ED using provided cli as {$CHROME_CLI} default {$Chrome_Default_Cli}
    eval "$BIN $Chrome_Default_Cli $CHROME_CLI $Log_Op";
else
    u _ED using default chromium cli as {$Chrome_Default_Cli};
    eval "$BIN $Chrome_Default_Cli $Log_Op"
fi;